package com.example.utils;

import java.util.UUID;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;


/**
 * @author ljs
 * @date 2025/7/17
 */
@Slf4j
public class RabbiMqSendUtil <T> {

    public static <T> void send(RabbitTemplate rabbitTemplate,
                                T data,
                                String exchange,
                                String routingKey,
                                MessageDeliveryMode businessType ) {
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());

        rabbitTemplate.convertAndSend(exchange,
                routingKey,
                data,
                message -> {
                    message.getMessageProperties().setDeliveryMode(businessType);
                    return message;
                },
                correlationData);

        correlationData.getFuture().addCallback(
                result -> {
                    if (result.isAck()) {
                        log.info("消息发送成功: {}", correlationData.getId());
                    } else {
                        log.error("消息发送失败: {}", correlationData.getId());
                    }
                },
                ex -> log.error("消息发送异常", ex)
        );
    }
}
